package com.ysd.servlet;

import java.io.IOException;

import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import com.ysd.dao.impl.userr;
import com.ysd.entity.use;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

@WebServlet("/userrr")
public class userrr extends BaseServlet {
	
	userr dao=new userr();
 
		public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			ArrayList<use> ls=dao.getUsers();
			request.setAttribute("us", ls);
			request.getRequestDispatcher("userlong.jsp").forward(request, response);
		}    
	     public void selectAllJson(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				 int page=Integer.parseInt(request.getParameter("page").toString());
				 int size=Integer.parseInt(request.getParameter("limit").toString());
				 String userName=null;
				 if(request.getParameter("userName")!=null && request.getParameter("userName")!="") {
					userName=request.getParameter("userName").toString(); 
				 }
					ArrayList<use> ls=dao.getUsers();
					ArrayList<use> lsdata=dao.getAllUsersPage(page, size,userName);
					Result res=new Result(0, "", ls.size(), lsdata);
				Gson g=new Gson();
				PrintWriter out=response.getWriter();
				out.print(g.toJson(res));
				out.flush();
				out.close();
			}  
				public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
					
				String userName=request.getParameter("userName").toString();
				String pwd=request.getParameter("pwd").toString();
				int age=Integer.parseInt(request.getParameter("age").toString());
				int i=dao.add(userName, pwd, age);
				Result res=new Result(200, "true", i, "success");
				Gson g=new Gson();
				PrintWriter out=response.getWriter();
				out.print(g.toJson(res));
				out.flush();
				out.close();
				}  	
				public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				int age=Integer.parseInt(request.getParameter("age").toString());
				int uid=Integer.parseInt(request.getParameter("uid").toString());
				int i=dao.update(age,uid); 
				Result res=new Result(200, "true", i, "success");
				Gson g=new Gson();
				PrintWriter out=response.getWriter();
				out.print(g.toJson(res));
				out.flush();
				out.close();
				}
				public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
					PrintWriter out=response.getWriter();
				int uid=Integer.parseInt(request.getParameter("uid").toString());
				int i=dao.delete(uid);
				Result res=new Result(200, "", i, "success");
				Gson g=new Gson();
				out.print(g.toJson(res));
				out.flush();
				out.close();
				}
				}

